package com.yixun.qfbao.annotation;

import static net.sf.oval.Validator.getCollectionFactory;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.oval.Validator;
import net.sf.oval.configuration.annotation.AbstractAnnotationCheck;
import net.sf.oval.context.OValContext;
import net.sf.oval.exception.OValException;


public class TYWValidCheck extends AbstractAnnotationCheck<TYWValid> {
	private Log logger = LogFactory.getLog(TYWValidCheck.class);
	private static final long serialVersionUID = -7845423901320035999L;

	private String regModel;
	private boolean required;
	private int min;
	private int max;
	private String errorMsg;
	
	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public String getRegModel() {
		return regModel;
	}

	public void setRegModel(String regModel) {
		this.regModel = regModel;
		requireMessageVariablesRecreation();
	}

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public boolean isRequired() {
		return required;
	}

	public void setRequired(boolean required) {
		this.required = required;
	}

	@Override
	public void configure(TYWValid constraintAnnotation) {
		super.configure(constraintAnnotation);
		setRegModel(constraintAnnotation.regModel());
		setRequired(constraintAnnotation.required());
		setMin(constraintAnnotation.min());
		setMax(constraintAnnotation.max());
	}
	
	@Override
	public void setMessage(String message) {
		if(message!=null&&!"".equals(message)){
			super.setMessage(message);
		}else{
			super.setMessage("com.dsa.message.error.model");
		}
	}
	
	@Override
	protected Map<String, String> createMessageVariables(){
		final Map<String, String> messageVariables = getCollectionFactory().createMap(1);
		messageVariables.put("max", Integer.toString(max));
		messageVariables.put("min", Integer.toString(min));
		return messageVariables;
	}
	
	public boolean isSatisfied(Object validatedObject, Object valueToValidate,OValContext context, Validator validator) throws OValException {
		try {
			if(valueToValidate==null||"".equals(valueToValidate.toString())){
				if(required||min>0){
					setMessage(Constants.loadMsgKeyBy(Constants.NOT_EMPTY));
					return false;
				}
				return true;
			}
			
			if(!Constants.XSS.equals(regModel)){
				String modelReg = Constants.loadRegBy(regModel);
				setMessage(Constants.loadMsgKeyBy(regModel));
				
				boolean isformate = valueToValidate.toString().matches(modelReg);
				
				if(required&&!isformate){
					return false;
				}else if(!isformate){
					logger.warn("MasapayValid Failed:[value="+valueToValidate+",regModel="+regModel+"]");
				}
			}
			
			if(max>=min&&(max!=Integer.MAX_VALUE||min!=0)){
				setMessage("net.sf.oval.constraint.Size.violated");
				if(!valueToValidate.toString().matches("[\\s\\S]{"+getMin()+","+getMax()+"}")){
					return false;
				};
			}
			
//			modelReg = Constants.loadRegBy(Constants.XSS);
//			setMessage(Constants.loadMsgKeyBy(Constants.XSS));
//			
//			return !valueToValidate.toString().matches(modelReg);

		} catch (Exception e) {
			logger.error("masapay验证: {}",e);
		}
		return true;
	}

}
